package com.itheima.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.mapper.SmsFlashPromotionMapper;
import com.itheima.mapper.SmsFlashPromotionProductRelationMapper;
import com.itheima.mapper.SmsFlashPromotionSessionMapper;
import com.itheima.pojo.SmsFlashPromotionSession;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 *
 * 秒杀时间段列表
 */

@DubboService
public class SmsFlashPromotionSessionImpl implements SmsFlashPromotionSessionApi{


    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;

    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    @Override
    public void deleteByIds(Integer id) {
        smsFlashPromotionSessionMapper.deleteById(id);
    }

    @Override
    public boolean save(SmsFlashPromotionSession smsFlashPromotionSession) {
        boolean flag = false;

        List<SmsFlashPromotionSession> list = listAll();
        for (SmsFlashPromotionSession role : list) {
            if (StringUtils.pathEquals(role.getName(), smsFlashPromotionSession.getName())) {
                flag = true;
                return flag;
            }
        }
        LocalDateTime localDateTime = LocalDateTime.now();
        smsFlashPromotionSession.setCreateTime(localDateTime);
        smsFlashPromotionSessionMapper.insert(smsFlashPromotionSession);

        return flag;
    }

    @Override
    public void updateStatus(Integer status, Integer id) {

        //获取场次ID
      SmsFlashPromotionSession session=new SmsFlashPromotionSession();
      session.setStatus(status);
        smsFlashPromotionSessionMapper.update(session,new LambdaQueryWrapper<SmsFlashPromotionSession>()
                .eq(SmsFlashPromotionSession::getId,id));
    }

    @Override
    public List<SmsFlashPromotionSession> listAll() {
        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionMapper.selectList(new LambdaQueryWrapper<SmsFlashPromotionSession>().ne(SmsFlashPromotionSession::getId,0));
        return list;
    }

    @Override
    public SmsFlashPromotionSession findById(Integer id) {
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionMapper.selectById(id);
        return smsFlashPromotionSession;
    }

    @Override
    public void updateSession(Integer id, SmsFlashPromotionSession smsFlashPromotionSession) {
        LambdaUpdateWrapper<SmsFlashPromotionSession> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(SmsFlashPromotionSession::getId,id);
        smsFlashPromotionSessionMapper.update(smsFlashPromotionSession,wrapper);
    }


    /**
     * 获取全部可选场次及其数量
     * @param flashPromotionId
     * @return
     */
    @Override
    public Map selectList(Integer flashPromotionId) {
//        Map<Long, Long> selectCount = smsFlashPromotionProductRelationMapper.selectCount(flashPromotionId);
//        System.out.println(selectCount);
        return null;
    }
}
